/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

/* eslint-disable eqeqeq */
import type { Effect, Reducer } from 'umi';
import { releaseNotice, getNoticeDetail } from '@/services/inn';
import { message } from 'antd';
import type { TableListItem } from '../types/NoticeVerify';

export interface NoticeVerifyStateType {
  prevNextNotice: TableListItem;
}

export interface NoticeVerifyModelType {
  namespace: string;
  state: NoticeVerifyStateType;
  effects: {
    releaseNotice: Effect;
    getNoticeDetail: Effect;
  };
  reducers: {
    save: Reducer<NoticeVerifyStateType>;
  };
}

const Model: NoticeVerifyModelType = {
  namespace: 'noticeVerify',

  state: {
    prevNextNotice: {},
  },

  effects: {
    *releaseNotice({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(releaseNotice, payload);
      if (!response) return;
      const { status } = payload;
      const { code, msg } = response;
      if (code == 0) {
        if (status === 3) {
          message.success('已同意发布');
        } else {
          message.success('已拒绝发布');
        }
        __dva_resolve(response);
      } else {
        message.error(msg);
        __dva_reject(response);
      }
    },
    *getNoticeDetail({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getNoticeDetail, payload);
      if (!response) return;
      const { code, msg, data } = response;
      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            prevNextNotice: data,
          },
        });
        __dva_resolve(response);
      } else {
        message.error(msg);
        __dva_reject(response);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};

export default Model;
